package com.example.exercise.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by haoxy on 2019/1/4.
 * E-mail:hxyHelloWorld@163.com
 * github:https://github.com/haoxiaoyong1014
 */
//TextWebSocketFrame 在 netty 中是用于webSocket专门处理文本对象,frame是消息的载体.
public class ChatHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {


    private final static Logger LOGGER = LoggerFactory.getLogger(ChatHandler.class);

    //用于记录和管理所有客户端的Channel

    private static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:MM");

    //当接收到客户端发过来的消息就会触发此回调
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception {
        String content = textWebSocketFrame.text();
        LOGGER.info("接收到的数据: " + content);
        //将消息发给其他客户端排除自己

        Channel channel = channelHandlerContext.channel();
        for (Channel ch : channels) {
            System.err.println(channel != ch);
            //排除当前通道
//            if (channel != ch) {
                ch.writeAndFlush(new TextWebSocketFrame(sdf.format(new Date()) + ":" + content));
//            }
        }

    }

    //当有新的客户端连接服务器之后,就会自动调用这个方法
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        channels.add(ctx.channel());
    }

}
